<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>事件传播机制示例</title>
  <style>
    #parent {
      width: 300px;
      height: 300px;
      background-color: #e3f2fd;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 8px;
      transition: background-color 0.3s;
    }
    #parent:hover {
      background-color: #bbdefb;
    }
    #child {
      padding: 12px 24px;
      font-size: 16px;
      cursor: pointer;
      border: none;
      border-radius: 4px;
      background-color: #2196f3;
      color: white;
      transition: all 0.3s;
    }
    #child:hover {
      background-color: #1976d2;
      transform: scale(1.05);
    }
  </style>
</head>
<body>
  <div id="parent">
    <button id="child">点击测试事件传播</button>
  </div>

  <script>
    const parent = document.getElementById("parent");
    const child = document.getElementById("child");

    // 捕获阶段处理程序
    parent.addEventListener("click", function(event) {
      const { target, currentTarget } = event;
      console.log("捕获阶段 - 父元素被点击");
      console.log(`事件目标: ${target.id}`);
      console.log(`当前目标: ${currentTarget.id}`);
    }, true);

    // 冒泡阶段处理程序
    child.addEventListener("click", function(event) {
      const { target, currentTarget } = event;
      console.log("冒泡阶段 - 子元素被点击");
      console.log(`事件目标: ${target.id}`);
      console.log(`当前目标: ${currentTarget.id}`);

      // 阻止事件进一步传播
      event.stopPropagation();
    });

    // 添加视觉反馈
    parent.addEventListener("click", function() {
      this.style.backgroundColor = "#90caf9";
      setTimeout(() => {
        this.style.backgroundColor = "#e3f2fd";
      }, 200);
    }, true);
  </script>
</body>
</html>
